using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public static class LevelEntityExtension
    {
        public static LevelMoveComponent GetMoveComponent(this LevelPlayBaseEntity entity)
        {
            return entity.GetComponent<LevelMoveComponent>(ETComponentType.EntityMove);
        }

        public static EntityUIViewComponent GetViewComponent(this LevelPlayBaseEntity entity)
        {
            return entity.GetComponent<EntityUIViewComponent>(ETComponentType.EntityUIView);
        }

        public static LevelAnimationComponent GetAnimationComponent(this LevelPlayBaseEntity entity)
        {
            return entity.GetComponent<LevelAnimationComponent>(ETComponentType.EntityAnimation);
        }

        public static LevelElevatorComponent GetLevelElevatorComponent(this LevelPlayBaseEntity entity)
        {
            return entity.GetComponent<LevelElevatorComponent>(ETComponentType.LevelElevator);
        }

        public static LevelSimpleStateComponent GetLevelSimpleStateComponent(this LevelPlayBaseEntity entity)
        {
            return entity.GetComponent<LevelSimpleStateComponent>(ETComponentType.LevelSimpleState);
        }

        public static LevelElevatorComponent GetElevatorComponent(this LevelPlayBaseEntity entity)
        {
            return entity.GetComponent<LevelElevatorComponent>(ETComponentType.LevelElevator);
        }

        public static LevelEntityAudioComponent GetAudioComponent(this LevelPlayBaseEntity entity)
        {
            return entity.GetComponent<LevelEntityAudioComponent>(ETComponentType.EntityAudio);
        }

        public static BVHBounds2D GetBVHBounds(this LevelPlayBaseEntity entity, RectInt rect, bool isRotated = false)
        {
            var unityPos = entity.GetUnityVec2Pos();
            var pos = unityPos + GameMathUtil.GetRectOffsetV2(rect);
            var size = GameMathUtil.Rect2OBBSizeV2(rect);

            if (isRotated)
            {
                var rotationY = GameMathUtil.GetRotationYFromInt(entity.dirEuler);
                return BVHBounds2D.FromPosSizeRotationY(pos, size, rotationY);
            }

            return BVHBounds2D.FromPosSize(pos, size);
        }

        public static bool IsOfferingQuest(this LevelNormalEntity entity)
        {
            if (entity.GetDynLevelEntity().levelEntityConfig.paramMap.TryGetValue(EntityDynamicParamType.QuestList, out var dynParam))
            {
                var quests = (dynParam as EntityParamQuest).offerQuests;
                foreach (int cid in quests)
                {
                    if (QuestModule.Instance.IsAcceptable(cid, entity))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}